using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using MyCollections.Model;
using MyCollections.Utils;

namespace MyCollections.Forms
{
    public partial class addDynamicData : Form
    {
        private List<FileMovie> ficheiros;
        private string validTypes;

        public addDynamicData()
        {

            ficheiros = new List<FileMovie>();
            InitializeComponent();


            //FIXME: only to test
            this.path.Text = "L:\\Projectos\\MyCollections\\MyCollections\\New Folder";
            this.validTypes = "*.avi";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog.ShowDialog().Equals(DialogResult.OK))
            {
                path.Text = this.folderBrowserDialog.SelectedPath;
            }
        }

        private void bt_close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            this.setInitialData();
        }

        public void setInitialData()
        {
            this.path.Text = "";

            this.bt_add.Enabled = false;
            bt_edit_row.Enabled = false;
            bt_remove.Enabled = false;

            type_avi.Checked = false;
            type_exe.Checked = false;
            type_mp3.Checked = false;
            type_mpeg.Checked = false;
            type_other.Checked = false;

            dataGrid.Rows.Clear();
        }

        private void path_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.path.Text))
                this.bt_load_data.Enabled = false;
            else
                this.bt_load_data.Enabled = true;
        }

        private ArrayList getFileTypes()
        {
            ArrayList ret = new ArrayList();

            if (type_avi.Checked)
                ret.Add(".avi");
            if (type_exe.Checked)
                ret.Add(".exe");
            if (type_mp3.Checked)
                ret.Add(".mp3");
            if (type_mpeg.Checked)
                ret.Add(".mpeg");
            if (type_pdf.Checked)
                ret.Add(".pdf");
            if (type_other.Checked)
            {
                string[] others = type_other_tb.Text.Split(',');
                string type;
                foreach (string s in others)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        type = s;
                        type = type.Replace(" ", "");
                        type = type.Replace("*", "");
                        if (!type.Substring(0, 1).Equals("."))
                            type = "." + type;
                        ret.Add(type);
                    }
                }
            }

            return ret;
        }

        private void bt_load_data_Click(object sender, EventArgs e)
        {
            ArrayList types = getFileTypes();

            ArrayList aux = Data.GenerateFileList(this.path.Text);
            ArrayList validFile = new ArrayList();
            FileMovie m;
            FileInfo fi;

            foreach (string s in aux)
            { 
                m = new FileMovie();
                fi = new FileInfo(s);

                if (types.Contains(fi.Extension))
                {
                    m.movie = ImdbTools.getImdbInfo(fi.Name.Split('.')[0]);

                    m.filename = fi.Name;
                    m.size = fi.Length.ToString();
                    m.valid = false;

                    ficheiros.Add(m);
                }
            }

            int i = 0;

            foreach (FileMovie f in ficheiros)
            {
                dataGrid.Rows.Add();
                dataGrid.Rows[i].Cells["filename"].Value = f.movie.title;
                dataGrid.Rows[i].Cells["size"].Value = f.size;
                dataGrid.Rows[i].Cells["link"].Value = f.movie.url;

                Image img = Image.FromStream(System.Net.HttpWebRequest.Create(f.movie.pictureurl).GetResponse().GetResponseStream());

                dataGrid.Rows[i].Cells["image"].Value = img;

                i++;
            }

            bt_edit_row.Enabled = true;
            bt_remove.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            addMovie window = new addMovie();
            if (window.ShowDialog() == DialogResult.OK)
            {
                dataGrid.Rows[dataGrid.CurrentCell.RowIndex].Cells["filename"].Value = ((TextBox)window.Controls["gb_moviedata"].Controls["title"]).Text;
                dataGrid.Rows[dataGrid.CurrentCell.RowIndex].Cells["image"].Value = ((PictureBox)window.Controls["gb_Imdb"].Controls["pb"]).Image;
                dataGrid.Rows[dataGrid.CurrentCell.RowIndex].Cells["link"].Value = ((TextBox)window.Controls["gb_Imdb"].Controls["imdb_url"]).Text;
            }
        }

        private void bt_remove_row_Click(object sender, EventArgs e)
        {
            dataGrid.Rows.RemoveAt(dataGrid.CurrentCell.RowIndex);
        }

        private void type_other_CheckedChanged(object sender, EventArgs e)
        {
            if (type_other.Checked)
                type_other_tb.Enabled = true;
            else
                type_other_tb.Enabled = false;
        }
    }
}